﻿Imports System
Imports System.Drawing
Imports SlimDX
Imports SlimDX.DirectInput


Namespace Engine

    Public Class Input


#Region "Members"
        Private kbDevice As SlimDX.DirectInput.Keyboard = New Keyboard(New DirectInput)
        Private mouseDevice As SlimDX.DirectInput.Mouse = New Mouse(New DirectInput)
        Private mouse As SlimDX.DirectInput.MouseState
        Private kbState As SlimDX.DirectInput.KeyboardState


        'Private DXDirectInput As New DirectInput
        'Private SharpDXKeyboard As Object
        'Private kbDevice As KeyboardState

        Private keyStates() As Boolean = {False, False, False, False, False, False}
        Private mouseStates() As Boolean = {False, False, False, False}
        Private vecPos As Vector3

        Public matView As Matrix
        Public pMatView As Matrix

        Public MousePos As Point ' PointF

#End Region
#Region "Properties"
        Public Function getKeyStates() As Boolean()
            Return keyStates
        End Function

#End Region
#Region "Methods"
        Public Sub New(handle As System.IntPtr, device As SlimDX.Direct3D9.Device)

            vecPos = New Vector3(450.0!, 350.0!, 750.0!)
            '  kbDevice = New SlimDX.DirectInput.Device '< KeyboardState > (System.Guid.Keyboard)
            ' mouseDevice = New SlimDX.DirectInput.Device '< MouseState > (SystemGuid.Mouse)
            '//mouseDevice.Properties.AxisModeAbsolute = false 



            kbDevice.SetCooperativeLevel(handle, CooperativeLevel.Nonexclusive Or CooperativeLevel.Background)
            mouseDevice.SetCooperativeLevel(handle, CooperativeLevel.Nonexclusive Or CooperativeLevel.Background)
            kbDevice.Acquire()
            mouseDevice.Acquire()
        End Sub

        Private Sub PollKeyboard()
            kbState = kbDevice.GetCurrentState
            For i As Integer = 0 To 5
                keyStates(i) = False
            Next

            For Each k As SlimDX.DirectInput.Key In kbState.PressedKeys
                Select Case k
                    Case Key.UpArrow
                        keyStates(0) = True
                    Case Key.DownArrow
                        keyStates(1) = True
                    Case Key.LeftArrow
                        keyStates(2) = True
                    Case Key.RightArrow
                        keyStates(3) = True
                    Case Key.A
                        keyStates(4) = True
                    Case Key.S
                        keyStates(5) = True
                End Select
            Next

        End Sub

        Private Sub PollMouse()
            mouseDevice.Poll()
            mouse = mouseDevice.GetCurrentState
            If mouse.IsPressed(0) Then
                MousePos.X = mouse.X
                MousePos.Y = mouse.Y


                For i As Integer = 0 To 3
                    mouseStates(i) = False
                    If (mouse.X > 0) Then mouseStates(0) = True
                    If (mouse.X < 0) Then mouseStates(1) = True
                    If (mouse.Y > 0) Then mouseStates(2) = True
                    If (mouse.Y < 0) Then mouseStates(3) = True
                Next
            End If


        End Sub

        Public Sub UpdateCamera(ByRef device As SlimDX.Direct3D9.Device)

            PollMouse()
            PollKeyboard()
            If keyStates(0) Or mouseStates(2) Then vecPos.Y += 5
            If keyStates(1) Or mouseStates(3) Then vecPos.Y -= 5
            If keyStates(2) Or mouseStates(0) Then vecPos.X += 5
            If keyStates(3) Or mouseStates(1) Then vecPos.X -= 5
            If keyStates(4) Then vecPos.Z += 5
            If keyStates(4) Then vecPos.Z -= 5

            ' Update the time and angle.
            'time = Environment.TickCount / 1000
            'angle = (time * (2.0F * Math.PI) / 1000.0F)

            matView = Matrix.LookAtLH(New Vector3(vecPos.X, vecPos.Y, vecPos.Z), _
                                                      New Vector3(0.0!, 0.0!, 0.0!), _
                                                      New Vector3(0.0!, 1.0!, 0.0!))

            device.SetTransform(SlimDX.Direct3D9.TransformState.View, matView)

        End Sub

        Public Sub Dispose()

            kbDevice.Unacquire()
            kbDevice.Dispose()
            mouseDevice.Unacquire()
            mouseDevice.Dispose()

        End Sub

#End Region

    End Class


End Namespace
